package uni.androidgame.gameobjects;


import android.graphics.Color;
import android.util.Log;

import java.util.ArrayList;

import uni.androidgame.mapgeneration.BSPMapNode;
import uni.androidgame.mapgeneration.Corridor;
import uni.androidgame.mapgeneration.MapGenerator;

public class DungeonMap extends BSPMapNode
{

    public static int TileSize = 32;

    public DungeonMap(int width, int height)
    {
        super(0,0,width,height,0,null);
    }


    public Tile getTouchedTile(float windowX, float windowY)
    {
        if(windowX<getWidth()*TileSize && windowY<getHeight()*TileSize)
        {
            return getTile((int)(windowX/(float)TileSize),(int)(windowY/(float)TileSize),this);
        }

        return null;
    }

    private Tile getTile(int x, int y,BSPMapNode parent)
    {
        if(parent.isLeaf())
        {
            return parent.getRoom().getTile(x,y);
        }
        for(Corridor c : parent.getCorridors())
        {
            Tile t = c.getTile(x,y);
            if(t!= null)
            {
                return t;
            }
        }

        if(parent.wasVerticallySplit())
        {
            if(parent.getLeftNode().getX()<=x && parent.getLeftNode().getX()+parent.getLeftNode().getWidth() > x)
            {
                return getTile(x,y,parent.getLeftNode());
            }
            else
            {
                return getTile(x,y,parent.getRightNode());
            }
        }
        else
        {
            if(parent.getLeftNode().getY()<=y && parent.getLeftNode().getY()+parent.getLeftNode().getHeight() > y)
            {
                return getTile(x,y,parent.getLeftNode());
            }
            else
            {
                return getTile(x,y,parent.getRightNode());
            }
        }
    }

    public Player createPlayer()
    {
        Tile emptyTile = getRandomEmptyTile();
        if(emptyTile == null)
        {
            Log.w("Creating player","No empty tile found - Aborting");
        }
        else
        {
            return new Player(emptyTile,2,20,Color.BLUE);
        }
        return null;
    }
    public ArrayList<Monster> createMonsters(int count)
    {
        ArrayList<Monster> newMonsters = new ArrayList<Monster>();
        for(int i =0;i<count;i++)
        {
            Tile emptyTile = getRandomEmptyTile();
            if(emptyTile == null)
            {
                Log.w("Creating monsters","No empty tile could be found - Aborting");
                return null;
            }
            else
            {
                newMonsters.add(new Monster(emptyTile,"Monster",1,4,Color.RED));
            }

        }
        return newMonsters;
    }

    public Tile getRandomEmptyTile()
    {
        return getRandomEmptyTile(this);
    }
    private Tile getRandomEmptyTile(BSPMapNode parent)
    {
        if(parent.isLeaf())
        {
            int randX = MapGenerator.getRandom(parent.getRoom().getX()+1,parent.getRoom().getRightSide()-1);
            int randY = MapGenerator.getRandom(parent.getRoom().getY()+1,parent.getRoom().getBottomSide()-1);
            return parent.getRoom().getTile(randX,randY);
        }
        if(Math.random()>0.5f)
        {
            return getRandomEmptyTile(parent.getLeftNode());
        }
        else
        {
            return getRandomEmptyTile(parent.getRightNode());
        }
    }


}
